package com.dc.project.service.impl;

import com.dc.base.exception.BusinessException;
import com.dc.base.util.AESUtil;
import com.dc.common.constants.ConstantsCommon;
import com.dc.common.model.BaseModel;
import com.dc.common.verification.RegexUtil;
import com.dc.project.mapper.StudentUserMapper;
import com.dc.project.model.StudentUser;
import com.dc.project.model.StudentUserInfo;
import com.dc.project.service.StudentUserService;
import com.dc.system.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: StudentUserServiceImpl
 * @Author: ChenXueLi
 * @Date: 2018-05-07 14:27
 */
@Service
public class StudentUserServiceImpl implements StudentUserService {

    @Autowired
    private StudentUserMapper studentUserMapper;

    /**
     * 新增学生用户（测试用）
     * @param studentUser
     * @param baseModel
     * @param request
     * @return
     */
    @Override
    public BaseModel addStudentUser(StudentUser studentUser, BaseModel baseModel, HttpServletRequest request) throws Exception{
        return baseModel;
    }

    /**
     * 学生用户登录
     * @param studentUser
     * @param baseModel
     * @param request
     * @return
     */
    @Override
    public BaseModel login(StudentUser studentUser, BaseModel baseModel, HttpServletRequest request) throws Exception{
        if(RegexUtil.isNull(studentUser.getAccount())) {
            throw new BusinessException(-101, "用户账号不能为空！");
        }
        if(RegexUtil.isNull(studentUser.getPassword())) {
            throw new BusinessException(-102, "密码不能为空！");
        }
        // 密码加密
        studentUser.setPassword(AESUtil.encrypt(studentUser.getPassword()));
        // 根据用户学生账号以及密码获取用户信息
        StudentUser tempStudentUser = studentUserMapper.getStudentUserByAccountAndPwd(studentUser);

        // 判断学生用户是否存在
        if (tempStudentUser == null) {
            throw new BusinessException(-103, "请输入正确的用户名和密码");
        } else {
            baseModel.setMessage("登录成功");
            baseModel.setCode(0);
            baseModel.setData(tempStudentUser);
        }

        //把用户信息放入session中
        request.getSession().setAttribute(ConstantsCommon.LOGIN_INFO, baseModel.getData());
        return baseModel;
    }

    /**
     * 学生用户修改密码
     * @param studentUser
     * @param baseModel
     * @param request
     * @return
     */
    @Override
    public BaseModel updateStudentUserPwdByAccount(StudentUser studentUser, BaseModel baseModel, HttpServletRequest request) throws Exception{

        return baseModel;
    }

    /**
     * 学生用户修改个人信息
     * @param studentUserInfo
     * @param baseModel
     * @param request
     * @return
     */
    @Override
    public BaseModel updateStudentUserInfo(StudentUserInfo studentUserInfo, BaseModel baseModel, HttpServletRequest request) throws Exception{
        return baseModel;
    }

}
